package com.dxl.controller;

import com.dxl.entity.Mail;
import com.dxl.entity.ResponseData;
import com.dxl.utils.EmailUtil;
import com.dxl.utils.ResDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

/**
 * @author dxl
 * @date 2020/12/18 15:00
 */
@Api(tags = "邮件发送模块")
@RestController
@RequestMapping("/mail")
@Validated
public class SendMailController {
    @Autowired EmailUtil emailUtil;
    /**
     * 发送邮件
     * @return
     */
    @ApiOperation(value = "发送邮件")
    @ResponseBody
    @GetMapping("/sendTemplate")
    public ResponseData send(
            @ApiParam(value = "收件地址",defaultValue = "870188670@qq.com")
            @RequestParam(value = "mailAddress",required = false)
            @Email(message = "邮箱格式不正确")
            @NotBlank(message = "邮箱不能为空") String mailAddress ){
        try {
            Mail mail = new Mail();
            mail.setReceiver(mailAddress);
            mail.setSubject("测试简单邮件");
            mail.setText("测试简单内容");
            emailUtil.sendMail(mail);
            return ResDataUtil.success("发送成功");
        }catch (Exception e){
            return ResDataUtil.failure("发送失败");
        }

    }
}
